const Net = require('../../common');
Page({
  data: {
    onFocus: false, //textarea焦点是否选中
    isShowText: false, //控制显示 textarea 还是 text
    addressCentent: '', //详细地址
    chooseType: 1,
    pageType: 1,
    is_default: 0,
    value: [0, 0, 0],
    zhi: '',
    cityList: [
      [],
      [],
      []
    ],
    activeAddress: [{}, {}, {}],
    statusif: false,
    statusd: 1
  },
  onLoad: function (options) {
    var _this = this
    if (options.status == 2) {
      Net.data({
        pageType: options.status,
        id: Number(options.id)
      })
      _this.onAddressInfo({
        id: options.id
      })
    }
    _this.onCityList({})
    var userNumber = wx.getStorageSync('member')
    if (userNumber.mobile) {
      Net.data({
        userPhoneNumber: userNumber.mobile
      })
    }
    if (userNumber.member_info != null) {
      Net.data({
        userName: userNumber.member_info.realname
      })
    }
  },
  onReady: function () {},
  onShow: function () {},
  onHide: function () {},
  onUnload: function () {},
  onPullDownRefresh: function () {},
  onReachBottom: function () {},
  onShareAppMessage: function () {},
  inputDataChenge(e) {
    var _this = this,
      value = e.detail.value,
      id = e.currentTarget.id
    /**
     * 获取收货人
     */
    if (id == 1) {
      Net.data({
        userName: value
      })
    }
    /**
     * 获取手机号
     */
    if (id == 2) {
      var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
      if (value.length == 0) {
        wx.showToast({
          title: '输入的手机号为空',
          icon: 'none',
          duration: 1500
        })
        return false;
      } else if (value.length < 11) {
        wx.showToast({
          title: '手机号长度有误！',
          icon: 'none',
          duration: 1500
        })
        return false;
      } else if (!myreg.test(value)) {
        wx.showToast({
          title: '手机号有误！',
          icon: 'none',
          duration: 1500
        })
        return false;
      } else {
        //输入正确
        Net.data({
          userPhoneNumber: value
        })
      }
    }
  },
  choosearea() {
    this.showModal();
    Net.data({
      statusif: true
    })
  },
  //显示对话框
  showModal: function () {
    // 显示遮罩层
    var animation = wx.createAnimation({
      duration: 50,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(400).step()
    Net.data({
      animationData: animation.export(),
      showModalStatus: true
    })
    setTimeout(function () {
      animation.translateY(0).step()
      Net.data({
        animationData: animation.export()
      })
    }.bind(this), 50)
  },
  //隐藏对话框
  hideModal: function () {
    // 隐藏遮罩层
    var animation = wx.createAnimation({
      duration: 100,
      timingFunction: "linear",
      delay: 0
    })
    Net.data({
      isShowText: false,
      onFacus: true
    })
    this.animation = animation
    animation.translateY(200).step()
    Net.data({
      animationData: animation.export(),
    })
    setTimeout(function () {
      animation.translateY(0).step()
      Net.data({
        animationData: animation.export(),
        showModalStatus: false
      })
    }.bind(this), 100)
  },
  /**
   * 获取详细地址
   */
  onRemarkInput(e) {
    var value = e.detail.value;
    Net.data({
      addressCentent: value,
    });
  },
  /**
   * 获取省市县接口
   */
  onAddressInfo(data) {
    var _this = this,
      activeAddress = _this.data.activeAddress
    Net.link('v1/mallAddress/' + data.id + '/edit', data,
      function (res) {
        var list = res.data.data.region_path_id.split(','),
          citylist = res.data.data.region_path_name.split(',')
        for (let i in activeAddress) {
          activeAddress[i].id = list[i]
          activeAddress[i].city = citylist[i]
        }
        Net.data({
          activeAddress: activeAddress,
          zhi: res.data.data.region_path_name,
          userName: res.data.data.realname,
          userPhoneNumber: res.data.data.mobile,
          addressCentent: res.data.data.address,
          is_default: res.data.data.is_default,
        })
        _this.onEditCity(list)
      }, 'GET')
  },
  /**
   * 获取编辑省市县
   */
  onEditCity(e) {
    var _this = this,
      cityList = _this.data.cityList,
      value = _this.data.value
    console.log(e)
    Net.link('getRegion', {},
      function (res) {
        console.log(res)
        for (let i in res.data.data) {
          if (res.data.data[i].id == e[0]) {
            value[0] = i
          }
        }
        cityList[0] = res.data.data
        Net.link('getRegion', {
            parent_id: e[0]
          },
          function (res) {
            console.log(res)
            for (let i in res.data.data) {
              if (res.data.data[i].id == e[1]) {
                value[1] = i
              }
            }
            cityList[1] = res.data.data
            Net.link('getRegion', {
                parent_id: e[1]
              },
              function (res) {
                console.log(res)
                for (let i in res.data.data) {
                  if (res.data.data[i].id == e[2]) {
                    value[2] = i
                  }
                }
                cityList[2] = res.data.data
                Net.data({
                  chooseType: 3,
                  cityList: cityList,
                  value: value,
                  statusd: 0
                })
              },
              'GET')
          },
          'GET')
      },
      'GET')
  },
  /**
   * 获取省市县接口
   */
  onCityList(data) {
    var _this = this,
      cityList = _this.data.cityList
    Net.link('getRegion', data,
      function (res) {
        console.log(res, data, cityList)
        cityList[data.type ? data.type : 0] = res.data.data
        Net.data({
          cityList: cityList
        })
        Net.link('getRegion', {
            parent_id: cityList[0][0].id
          },
          function (res) {
            cityList[1] = res.data.data
            Net.data({
              cityList: cityList
            })
            Net.link('getRegion', {
                parent_id: cityList[1][0].id
              },
              function (res) {
                cityList[2] = res.data.data
                console.log('我要知道的数值', _this.data.value, cityList)
                Net.data({
                  cityList: cityList,
                  statusd: 0
                })
              },
              'GET')
          },
          'GET')
      },
      'GET')
  },
  /**
   * 获取点击的省市县
   */
  place11: function (e) {
    console.log(e)
    var that = this,
      cityList = that.data.cityList
    if (e.detail.column == 0) {
      wx.showToast({
        title: '获取地址中...',
        icon: 'loading'
      })
      Net.link('getRegion', {
          parent_id: cityList[0][e.detail.value].id
        },
        function (res) {
          cityList[1] = res.data.data
          that.setData({
            cityList: cityList
          });
          Net.link('getRegion', {
              parent_id: cityList[1][0].id
            },
            function (res) {
              cityList[2] = res.data.data
              that.setData({
                cityList: cityList,
                one: e.detail.value,
                value: [e.detail.value, 0, 0]
              });
              wx.hideToast()
            }, "GET")
        },"GET")
    } else if (e.detail.column == 1) {
      wx.showToast({
        title: '获取地址中...',
        icon: 'loading'
      })
      Net.link('getRegion', {
          parent_id: cityList[1][e.detail.value].id
        },
        function (res) {
          cityList[2] = res.data.data
          that.setData({
            cityList: cityList,
            value: [that.data.one, e.detail.value, 0]
          });
          wx.hideToast()
        }, 'GET')
    }
  },
  place: function (e) {
    var _this = this,
      l = _this.data.activeAddress
    l[0].id = this.data.cityList[0][e.detail.value[0]].id
    l[1].id = this.data.cityList[1][e.detail.value[1]].id
    l[2].id = this.data.cityList[2][e.detail.value[2]].id
    l[0].city = this.data.cityList[0][e.detail.value[0]].short_name
    l[1].city = this.data.cityList[1][e.detail.value[1]].short_name
    l[2].city = this.data.cityList[2][e.detail.value[2]].short_name
    Net.data({
      zhi: this.data.cityList[0][e.detail.value[0]].short_name + ' ' + this.data.cityList[1][e.detail.value[1]].short_name + ' ' + this.data.cityList[2][e.detail.value[2]].short_name,
      activeAddress: l
    })
    console.log(_this.data)
  },
  close: function () {
    this.hideModal();
    Net.data({
      isShowText: false,
      onFacus: true
    })
  },
  /**
   * 是否默认地址
   */
  checkboxChange(e) {
    console.log(e.detail.value)
    if (!e.detail.value) {
      Net.data({
        is_default: 0
      })
    } else {
      Net.data({
        is_default: 1
      })
    }
  },
  onSaveInfo() {
    var _this = this,
      activeAddress = _this.data.activeAddress,
      region_path_id = activeAddress[0].id + ',' + activeAddress[1].id + ',' + activeAddress[2].id,
      region_path_name = activeAddress[0].city + ',' + activeAddress[1].city + ',' + activeAddress[2].city,
      is_default = _this.data.is_default
    for (let i in activeAddress) {
      if (activeAddress[i].id == null) {
        wx.showToast({
          title: '请填写完整的收货地址!',
          icon: 'none'
        })
        return false
      }
    }
    var mobile = /^[1][3,4,5,7,8][0-9]{9}$/;
    var isMobile = mobile.exec(_this.data.userPhoneNumber);
    if (!isMobile) {
      wx.showToast({
        title: '请输入正确的手机号',
        icon: 'none'
      })
      return false
    }
    if (_this.data.userName == '') {
      wx.showToast({
        title: '收件人姓名不能为空!',
        icon: 'none'
      })
      return false
    }
    var data = {
      realname: _this.data.userName,
      mobile: _this.data.userPhoneNumber,
      region_path_id: region_path_id,
      region_path_name: region_path_name,
      address: _this.data.addressCentent,
      is_default: is_default
    }
    if (_this.data.pageType == 2) {
      data.id = _this.data.id
      Net.link('v1/mallAddress/' + _this.data.id, data,
        function (res) {
          console.log(res)
          wx.showToast({
            title: '修改成功',
            icon: 'success'
          })
          setTimeout(() => {
            wx.navigateBack({
              delta: 1
            })
          }, 1400);
        },'PUT  ')
      return false
    }
    console.log(data)
    Net.link('v1/mallAddress', data,
      function (res) {
        wx.showToast({
          title: '添加成功',
          icon: 'success'
        })
        setTimeout(() => {
          wx.navigateBack({
            delta: 1
          })
        }, 1400);
      })
  },

})